/* Copyright 2009 Requirement Management System
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.dao.provider;

import java.util.ArrayList; 
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.mags.dao.common.DAO;
import org.mags.dao.common.DatabaseContext;
import org.mags.dao.common.MDAO;
import org.mags.dao.common.DatabaseAccess;
import org.mags.dao.common.ProviderFactory;
import org.mags.dao.common.RestrictionBuilderFactory;

import java.io.Serializable;

/**
 * @author Mario Gait�n
 */

public class DataContextImp implements DatabaseContext, Serializable {
	private static final long serialVersionUID = 1L;
	private List<ProviderFactory> providerFactories;
	private RestrictionBuilderFactory restrictionBuilderCreator;
	private Map<String, Object> miscellaneous;
	private DAO dao;
	private MDAO mdao;

	@Override
	public Object getFromMiscellaneous(String key) {
		return getMiscellaneous().get(key);
	}
	private Map<String, Object> getMiscellaneous() {
		if(miscellaneous==null) 
			miscellaneous = new HashMap<String, Object>();
		return miscellaneous;
	}
	@Override
	public MDAO getMdao() {
		return mdao;
	}
	@Override
	public void setMdao(MDAO mdao) {
		this.mdao = mdao;
	}
	@Override
	public RestrictionBuilderFactory getRestrictionBuilderFactory() {
		return restrictionBuilderCreator;
	}
	@Override
	public void setRestrictionBuilderFactory(
			RestrictionBuilderFactory restrictionBuilderCreator) {
		this.restrictionBuilderCreator = restrictionBuilderCreator;
	}
	@Override
	public DAO getDao() {
		return dao;
	}
	@Override
	public void setDao(DAO dao) {		
		this.dao = dao;
	}	
	@Override
	public void refreshCache() {
		for(ProviderFactory i: getProviderFactories()) 
			i.refreshAll();
	}
	@Override
	public ProviderFactory getProviderFactory(String id) {
		for(ProviderFactory i: getProviderFactories()) 
			if(i.getID().equalsIgnoreCase(id)) 
				return i;
		return null;
	}
	@Override
	public Collection<String> getProviderFactoryIds() {
		List<String> ids = new ArrayList<String>();
		for(ProviderFactory i: getProviderFactories())
			ids.add(i.getID());
		return ids;
	}
	@Override
	public void addProviderFactory(Object factory) {
		if(factory instanceof ProviderFactory) {
			ProviderFactory _factory = (ProviderFactory)factory;
			_factory.setDataContext(this);
			addInList(_factory);
		} else if(factory instanceof ModuleProviderFactories) {
			ModuleProviderFactories module = (ModuleProviderFactories)factory;
			for(ProviderFactory pv: module.getFactories()) {
				pv.setDataContext(this);
				addInList(pv);
			}
		} 
		if(factory instanceof MiscellaneousFactory) {
			MiscellaneousFactory miscprov = (MiscellaneousFactory)factory;
			Map<String, Object> misc = miscprov.getMiscellaneous();
			for(Entry entry: misc.entrySet()) {
				DatabaseAccess m = (DatabaseAccess)entry.getValue();
				m.setDataContext(this);
				getMiscellaneous().put((String)entry.getKey(), m);
			}
		}
	}
	@Override
	public void setProviderFactories(Collection factories) {
		getProviderFactories().clear();
		for(Object i: factories) 
			addProviderFactory(i);
	}
	protected void addInList(ProviderFactory factory) {
		Collection<ProviderFactory> factories = getProviderFactories();
		if(!factories.contains(factory))
			factories.add(factory);
	}
	@Override
	public Collection<ProviderFactory> getProviderFactories() {
		if(providerFactories==null)
			providerFactories = new ArrayList<ProviderFactory>();
		return providerFactories;
	}
}
